<?php
function printCalendar($year) {
    $months = array(
        1 => "一月", 2 => "二月", 3 => "三月",
        4 => "四月", 5 => "五月", 6 => "六月",
        7 => "七月", 8 => "八月", 9 => "九月",
        10 => "十月", 11 => "十一月", 12 => "十二月"
    );
    
    echo "<h1>{$year}年年历</h1>";
    
    for ($month = 1; $month <= 12; $month++) {
        // 获取每月的天数
        $days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);
        // 获取每月第一天是星期几
        $first_day = date("w", mktime(0, 0, 0, $month, 1, $year));
        
        echo "<div style='margin: 20px; display: inline-block;'>";
        echo "<h3>{$months[$month]}</h3>";
        echo "<table border='1' style='border-collapse: collapse;'>";
        echo "<tr style='background-color: #f0f0f0;'>";
        echo "<th style='width: 40px;'>日</th>";
        echo "<th style='width: 40px;'>一</th>";
        echo "<th style='width: 40px;'>二</th>";
        echo "<th style='width: 40px;'>三</th>";
        echo "<th style='width: 40px;'>四</th>";
        echo "<th style='width: 40px;'>五</th>";
        echo "<th style='width: 40px;'>六</th>";
        echo "</tr><tr>";
        
        // 打印空白日期
        for ($i = 0; $i < $first_day; $i++) {
            echo "<td>&nbsp;</td>";
        }
        
        // 打印日期
        $day_count = $first_day;
        for ($day = 1; $day <= $days_in_month; $day++) {
            if ($day_count == 7) {
                echo "</tr><tr>";
                $day_count = 0;
            }
            echo "<td align='center'>$day</td>";
            $day_count++;
        }
        
        // 补齐最后一行的空白
        while ($day_count < 7) {
            echo "<td>&nbsp;</td>";
            $day_count++;
        }
        
        echo "</tr></table></div>";
    }
}
?>

<!DOCTYPE html>
<html>

<head>
  <title>年历</title>
  <meta charset="UTF-8">
  <style>
  body {
    font-family: Arial, sans-serif;
    margin: 20px;
  }

  table {
    margin: 10px;
  }

  td,
  th {
    padding: 5px;
    text-align: center;
  }

  h1 {
    text-align: center;
    color: #333;
  }

  h3 {
    color: #666;
    margin: 5px 0;
  }
  </style>
</head>

<body>
  <form method="post">
    <div style="text-align: center; margin: 20px;">
      输入年份：<input type="number" name="year" value="<?php echo date('Y'); ?>">
      <input type="submit" value="显示年历">
    </div>
  </form>

  <?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $year = isset($_POST["year"]) ? (int)$_POST["year"] : date("Y");
        printCalendar($year);
    }
    ?>
</body>

</html>